package com.ceair.flight.refund.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.ceair.flight.refund.dto.external.CreatePnrSegmentDto;
import com.ceair.flight.refund.dto.external.IataInfo;
import com.ceair.flight.refund.service.IRefundSupportService;
import com.ceair.flight.refund.util.Constant;
import com.ceair.flight.refund.util.ReturnMsg;
import com.ceair.muservice.basicdata.dto.airport.AirportDto;
import com.ceair.muservice.basicdata.dto.channel.ChannelManageDto;
import com.ceair.muservice.basicdata.dto.channel.ChannelRQ;
import com.ceair.muservice.basicdata.dto.config.ConfigDto;
import com.ceair.muservice.basicdata.feign.util.AirportUtil;
import com.ceair.muservice.basicdata.feign.util.ChannelUtil;
import com.ceair.muservice.basicdata.feign.util.ConfigUtil;
import com.ceair.muservice.common.exception.MuServiceException;
import com.google.gson.Gson;

/**
 * 退票公共服务
 *
 * @Author Chenglongyi
 */
@Service
public class RefundSupportServiceImpl implements IRefundSupportService {

    private static final Logger LOG = LoggerFactory.getLogger(RefundSupportServiceImpl.class);

//    @Autowired
//    @Qualifier("uniConfigManagerWS")
//    private IUniConfigManagerWS iUniConfigManagerWS;

    /**
     * 根据渠道号获取渠道信息
     *
     * @param salesChannel
     * @return
     */
    @Override
    public ChannelManageDto findChannelInfoByChannel(String salesChannel) {
        try {
            LOG.info("获取渠道信息开始，渠道：" + salesChannel);
            ChannelRQ channelRQ = new ChannelRQ();
            channelRQ.setChannelCode(salesChannel);
            ChannelManageDto channelDto = ChannelUtil.findChannelByChannelCode(salesChannel);
            if (channelDto == null) {
                LOG.info("获取渠道信息失败");
                return null;
            }
            LOG.info("获取渠道信息结束，出参：" + new Gson().toJson(channelDto));
            return channelDto;
        } catch (Exception e) {
            LOG.error("获取渠道信息失败" + e.getMessage(), e);
        }
        return null;
    }

    /**
     * 返回改期的最大次数, 查询失败默认返回10
     *
     * @return
     */
    @Override
    public int getMaxRescheduleTimes() {
        int temp = 10;
        try {
            LOG.info("获取改期最大次数开始");
            ConfigDto configDto = ConfigUtil.findConfigByCode(Constant.MAX_RESCHEDUEL_TIME);
            if (configDto == null) {
                LOG.info("查询可改期的最大次数失败，返回默认值 10 ");
                return temp;
            }
            String times = configDto.getCfgValue();
            temp = Integer.parseInt(times);
            LOG.info("查询可改期的最大次数:" + temp);
        } catch (Exception e) {
            LOG.info("查询可改期的最大次数失败，返回默认值 10 ");
        }
        return temp;
    }

    /**
     * 获取机场信息
     *
     * @param airportCode
     * @return
     */
    @Override
    public AirportDto getAirport(String airportCode) {
        LOG.info("获取机场信息开始，机场码：" + airportCode);
        AirportDto nationCityAirportDto;
        try {
            nationCityAirportDto = AirportUtil.findByCode(airportCode);
            LOG.info("获取机场信息结束,rs:" + JSON.toJSONString(nationCityAirportDto));
        } catch (Exception e) {
            LOG.error("获取机场信息异常,airportCode:" + airportCode);
            throw new MuServiceException(ReturnMsg.SERVICE_ERROR_315.getCode(), ReturnMsg.SERVICE_ERROR_315.getMsg());
        }
        return nationCityAirportDto;
    }

    /**
     * 获取EC渠道
     *
     * @param channelCode
     * @return
     */
    @Override
    public ChannelManageDto findEcChannelByChannel(Long channelCode) {
        LOG.info("获取EC渠道开始，渠道：" + channelCode);
        ChannelRQ channelRQ = new ChannelRQ();
        channelRQ.setChannelCode(String.valueOf(channelCode));
        ChannelManageDto channelDto = ChannelUtil.findChannelByChannelCode(String.valueOf(channelCode));
        if (channelDto == null) {
            LOG.info("获取EC渠道失败");
            throw new MuServiceException(ReturnMsg.SERVICE_ERROR_208.getCode(), ReturnMsg.SERVICE_ERROR_208.getMsg());
        }
        LOG.info("获取EC渠道结束，出参：" + new Gson().toJson(channelDto));
        return channelDto;
    }

    /**
     * 判断航段是否国际航段
     *
     * @param depAirPort
     * @param arrAirPort
     * @return
     */
    @Override
    public Boolean vfyInteaAirlineByAirportCode(String depAirPort, String arrAirPort) {
        LOG.info("查询航段类型开始，出发机场：" + depAirPort + " 到达机场：" + arrAirPort);
        List<String> airPortList = new ArrayList<>();
        airPortList.add(depAirPort);
        airPortList.add(arrAirPort);
        Boolean rs = AirportUtil.isInterByCodeList(airPortList);
        if (rs == null) {
            LOG.info("判断航段是否国际航段失败");
        }
        LOG.info("查询航段类型结束，是否是国际航线：" + rs);
        return rs;
    }

    /**
     * 查询结算信息
     *
     * @return
     */
    @Override
//    public Map<String, IataInfo> findUniFndConfigByCode() {
//        LOG.info("根据fndConfig 查询出的IATA结束 ");
//        UniFndConfigRS fndConfig = iUniConfigManagerWS.findUniFndConfigByCode(Constant.IATA_INFO);
//        LOG.info("fndConfigRs:" + new Gson().toJson(fndConfig));
//        if (!"0".equals(fndConfig.getMsgCode()) && null != fndConfig.getUniFndConfigVO()
//                && null != fndConfig.getUniFndConfigVO().getCode()) {
//            String s = fndConfig.getUniFndConfigVO().getCfgValue();
//            IataInfo[] array = new Gson().fromJson(s, IataInfo[].class);
//            Map<String, IataInfo> map = new HashMap<>();
//            for (IataInfo iataInfo : array) {
//                map.put(iataInfo.getIatano(), iataInfo);
//            }
//            return map;
//        }
//        throw new MuServiceException(ReturnMsg.SERVICE_ERROR_401.getCode(), ReturnMsg.SERVICE_ERROR_401.getMsg());
//    }
    public Map<String, IataInfo> findUniFndConfigByCode() {
        Map<String, IataInfo> map = new HashMap<>();
        return map;
    }

    /**
     * 通过机场获取国家代号
     *
     * @param arrAirp
     * @return
     */
    @Override
    public String queryAirpCity(String arrAirp) {
        LOG.info("查询国家代码开始，机场码：" + arrAirp);
        String nationCode = null;
        AirportDto nationCityAirportDto = null;
        try {
            nationCityAirportDto = AirportUtil.findByCode(arrAirp);
            nationCode = nationCityAirportDto.getNationCode();
            LOG.info("查询国家代码结束，国家码：" + nationCode);
        } catch (Exception e) {
            LOG.error("通过机场获取国家代号异常");
            throw new MuServiceException(ReturnMsg.SERVICE_ERROR_249.getCode(), ReturnMsg.SERVICE_ERROR_249.getMsg());
        }
        return nationCode;
    }

    /**
     * 获取航段中属于北美航段的
     *
     * @param pnrSegmentDto
     * @return
     */
    @Override
    public Boolean vfyNAAirlineByAirportCode(CreatePnrSegmentDto pnrSegmentDto) {
        LOG.info("查询航段是否是北美航段开始，入参：" + new Gson().toJson(pnrSegmentDto));
        List<String> airportList = new ArrayList<>();
        airportList.add(pnrSegmentDto.getArrAirp());
        airportList.add(pnrSegmentDto.getDepAirp());
        Boolean fndNAAirlineRS = AirportUtil.isNAByCodeList(airportList);
        if (fndNAAirlineRS == null) {
            LOG.error("判断航段是否属于北美航段失败" + pnrSegmentDto.getArrAirp());
            throw new MuServiceException(ReturnMsg.SERVICE_ERROR_250.getCode(), ReturnMsg.SERVICE_ERROR_250.getMsg());
        }
        LOG.info("查询航段是否是北美航段结束，是北美航线：" + fndNAAirlineRS);
        return fndNAAirlineRS;
    }


}
